//
//  RootTabViewController.m
//  MyApp
//
//  Created by sfpay on 14/12/30.
//  Copyright (c) 2014年 Zain. All rights reserved.
//

#import "RootTabViewController.h"
#import "TweetViewController.h"
#import "NewsViewController.h"
#import "TaskViewController.h"
#import "MessageViewController.h"
#import "MyViewController.h"

#import <RDVTabBarController/RDVTabBarItem.h>

@interface RootTabViewController()
@property (nonatomic, strong) TweetViewController *tweetVC;
@property (nonatomic, strong) NewsViewController *newsVC;
@property (nonatomic, strong) TaskViewController *taskVC;
@property (nonatomic, strong) MessageViewController *messageVC;
@property (nonatomic, strong) MyViewController *myVC;

@property (nonatomic, strong) RootNavController *newsRootNav;
@property (nonatomic, strong) RootNavController *tweetRootNav;
@property (nonatomic, strong) RootNavController *taskRootNav;
@property (nonatomic, strong) RootNavController *messageRootNav;
@property (nonatomic, strong) RootNavController *myRootNav;

@end

@implementation RootTabViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubControllers];
}

- (void) initSubControllers{
    
    //1. init controllers
    _newsVC = [[NewsViewController alloc] init];
    _newsVC.title = MyLocalize(@"project");
    
    _taskVC = [[TaskViewController alloc] init];
    _taskVC.title = MyLocalize(@"task");
    
    _tweetVC = [[TweetViewController alloc] init];
    _tweetVC.title = MyLocalize(@"tweet");
    
    _messageVC = [[MessageViewController alloc] init];
    _messageVC.title = MyLocalize(@"message");
    
    _myVC = [[MyViewController alloc] init];
    _myVC.title = MyLocalize(@"my");
    
    _newsRootNav = [[RootNavController alloc] initWithRootViewController:_newsVC];
    _tweetRootNav = [[RootNavController alloc] initWithRootViewController:_taskVC];
    _taskRootNav = [[RootNavController alloc] initWithRootViewController:_tweetVC];
    _messageRootNav = [[RootNavController alloc] initWithRootViewController:_messageVC];
    _myRootNav = [[RootNavController alloc] initWithRootViewController:_myVC];
    
    [self setViewControllers:@[_newsRootNav, _tweetRootNav,_taskRootNav, _messageRootNav, _myRootNav]];
    
    //2. setting controllers
    [self settingControllers];
    
    //3.
    self.delegate = self;
}

- (void) settingControllers {
    UIImage *backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    NSArray *tabbarItems = @[@"project", @"task", @"tweet", @"privatemessage", @"me"];
    NSArray *tabbarTitiles = @[MyLocalize(@"project"), MyLocalize(@"task"),MyLocalize(@"tweet"), MyLocalize(@"privatemessage"), MyLocalize(@"me")];
    
    int index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]) {
        [item setBackgroundSelectedImage:backgroundImage withUnselectedImage:backgroundImage];
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
                                                      [tabbarItems objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                        [tabbarItems objectAtIndex:index]]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        [item setTitle:[tabbarTitiles objectAtIndex:index]];
        
        index++;
    }
}

#pragma mark - delegate
- (void)tabBarController:(RDVTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[BaseViewController class]]) {
        if ([viewController respondsToSelector:@selector(tabBarItemClicked)]) {
            [viewController performSelectorOnMainThread:@selector(tabBarItemClicked) withObject:nil waitUntilDone:YES];
        }
    }
}

@end
